﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace GUITree
{
	// Token: 0x020004B9 RID: 1209
	[DisallowMultipleComponent]
	[ExecuteInEditMode]
	[RequireComponent(typeof(RectTransform))]
	[AddComponentMenu("GUITree/Tree Root", 1003)]
	public class TreeRoot : UIBehaviour, ITreeLayoutElement, ILayoutGroup, ILayoutElement, ILayoutController
	{
		// Token: 0x06001E7B RID: 7803 RVA: 0x000BFA68 File Offset: 0x000BDC68
		protected TreeRoot()
		{
		}

		// Token: 0x1700035E RID: 862
		// (get) Token: 0x06001E7C RID: 7804 RVA: 0x000BFA88 File Offset: 0x000BDC88
		protected RectTransform rectTransform
		{
			get
			{
				if (this.m_Rect == null)
				{
					this.m_Rect = base.GetComponent<RectTransform>();
				}
				return this.m_Rect;
			}
		}

		// Token: 0x1700035F RID: 863
		// (get) Token: 0x06001E7D RID: 7805 RVA: 0x000BFAB0 File Offset: 0x000BDCB0
		protected List<TreeNode> treeChildren
		{
			get
			{
				return this.m_TreeLayoutElement;
			}
		}

		// Token: 0x17000360 RID: 864
		// (get) Token: 0x06001E7E RID: 7806 RVA: 0x000BFAB8 File Offset: 0x000BDCB8
		// (set) Token: 0x06001E7F RID: 7807 RVA: 0x000BFAC0 File Offset: 0x000BDCC0
		public float spacing
		{
			get
			{
				return this.m_Spacing;
			}
			set
			{
				this.SetProperty<float>(ref this.m_Spacing, value);
			}
		}

		// Token: 0x06001E80 RID: 7808 RVA: 0x000BFAD0 File Offset: 0x000BDCD0
		protected void CalcAlongAxis(int axis)
		{
			float num = 0f;
			bool flag = true ^ axis == 1;
			for (int i = 0; i < this.treeChildren.Count; i++)
			{
				TreeNode element = this.treeChildren[i];
				float preferredSize = LayoutUtility.GetPreferredSize(element, axis);
				if (flag)
				{
					num = Mathf.Max(preferredSize, num);
				}
				else
				{
					num += preferredSize + this.spacing;
				}
			}
			if (!flag && this.treeChildren.Count > 0)
			{
				num -= this.spacing;
			}
			this.m_TotalPreferredSize[axis] = num;
		}

		// Token: 0x06001E81 RID: 7809 RVA: 0x000BFB6C File Offset: 0x000BDD6C
		public virtual void CalculateLayoutInputHorizontal()
		{
			this.m_TreeLayoutElement.Clear();
			for (int i = 0; i < this.rectTransform.childCount; i++)
			{
				TreeNode component = this.rectTransform.GetChild(i).GetComponent<TreeNode>();
				if (!(component == null) && component.IsActive())
				{
					this.m_TreeLayoutElement.Add(component);
				}
			}
			this.CalcAlongAxis(0);
		}

		// Token: 0x06001E82 RID: 7810 RVA: 0x000BFBE4 File Offset: 0x000BDDE4
		public void CalculateLayoutInputVertical()
		{
			this.CalcAlongAxis(1);
		}

		// Token: 0x17000361 RID: 865
		// (get) Token: 0x06001E83 RID: 7811 RVA: 0x000BFBF0 File Offset: 0x000BDDF0
		public virtual float minWidth
		{
			get
			{
				return this.preferredWidth;
			}
		}

		// Token: 0x17000362 RID: 866
		// (get) Token: 0x06001E84 RID: 7812 RVA: 0x000BFBF8 File Offset: 0x000BDDF8
		public virtual float preferredWidth
		{
			get
			{
				return this.m_TotalPreferredSize[0];
			}
		}

		// Token: 0x17000363 RID: 867
		// (get) Token: 0x06001E85 RID: 7813 RVA: 0x000BFC08 File Offset: 0x000BDE08
		public virtual float flexibleWidth
		{
			get
			{
				return this.preferredWidth;
			}
		}

		// Token: 0x17000364 RID: 868
		// (get) Token: 0x06001E86 RID: 7814 RVA: 0x000BFC10 File Offset: 0x000BDE10
		public virtual float minHeight
		{
			get
			{
				return this.preferredHeight;
			}
		}

		// Token: 0x17000365 RID: 869
		// (get) Token: 0x06001E87 RID: 7815 RVA: 0x000BFC18 File Offset: 0x000BDE18
		public virtual float preferredHeight
		{
			get
			{
				return this.m_TotalPreferredSize[1];
			}
		}

		// Token: 0x17000366 RID: 870
		// (get) Token: 0x06001E88 RID: 7816 RVA: 0x000BFC28 File Offset: 0x000BDE28
		public virtual float flexibleHeight
		{
			get
			{
				return this.preferredHeight;
			}
		}

		// Token: 0x17000367 RID: 871
		// (get) Token: 0x06001E89 RID: 7817 RVA: 0x000BFC30 File Offset: 0x000BDE30
		public virtual int layoutPriority
		{
			get
			{
				return 0;
			}
		}

		// Token: 0x06001E8A RID: 7818 RVA: 0x000BFC34 File Offset: 0x000BDE34
		public void SetLayoutHorizontal()
		{
			this.m_Tracker.Clear();
		}

		// Token: 0x06001E8B RID: 7819 RVA: 0x000BFC44 File Offset: 0x000BDE44
		public void SetLayoutVertical()
		{
			float num = 0f;
			for (int i = 0; i < this.treeChildren.Count; i++)
			{
				TreeNode treeNode = this.treeChildren[i];
				float preferredSize = LayoutUtility.GetPreferredSize(treeNode, 1);
				this.m_Tracker.Add(this, treeNode.rectTransform, DrivenTransformProperties.AnchoredPositionY);
				treeNode.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, num, preferredSize);
				num += preferredSize + this.spacing;
			}
		}

		// Token: 0x06001E8C RID: 7820 RVA: 0x000BFCB4 File Offset: 0x000BDEB4
		protected override void OnEnable()
		{
			base.OnEnable();
			this.SetDirty();
		}

		// Token: 0x06001E8D RID: 7821 RVA: 0x000BFCC4 File Offset: 0x000BDEC4
		protected override void OnDisable()
		{
			this.m_Tracker.Clear();
			if (this.rectTransform != null)
			{
				LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
			}
			base.OnDisable();
		}

		// Token: 0x06001E8E RID: 7822 RVA: 0x000BFCF4 File Offset: 0x000BDEF4
		protected override void OnDidApplyAnimationProperties()
		{
			this.SetDirty();
		}

		// Token: 0x17000368 RID: 872
		// (get) Token: 0x06001E8F RID: 7823 RVA: 0x000BFCFC File Offset: 0x000BDEFC
		private bool isRootLayoutGroup
		{
			get
			{
				Transform parent = base.transform.parent;
				return parent == null || base.transform.parent.GetComponent(typeof(ILayoutGroup)) == null;
			}
		}

		// Token: 0x06001E90 RID: 7824 RVA: 0x000BFD44 File Offset: 0x000BDF44
		protected override void OnRectTransformDimensionsChange()
		{
			base.OnRectTransformDimensionsChange();
			if (this.isRootLayoutGroup)
			{
				this.SetDirty();
			}
		}

		// Token: 0x06001E91 RID: 7825 RVA: 0x000BFD60 File Offset: 0x000BDF60
		protected virtual void OnTransformChildrenChanged()
		{
			this.SetDirty();
		}

		// Token: 0x06001E92 RID: 7826 RVA: 0x000BFD68 File Offset: 0x000BDF68
		protected void SetProperty<T>(ref T currentValue, T newValue)
		{
			if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
			{
				return;
			}
			currentValue = newValue;
			this.SetDirty();
		}

		// Token: 0x06001E93 RID: 7827 RVA: 0x000BFDC8 File Offset: 0x000BDFC8
		protected void SetDirty()
		{
			if (!this.IsActive())
			{
				return;
			}
			if (this.rectTransform != null)
			{
				LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
			}
		}

		// Token: 0x040020A7 RID: 8359
		private RectTransform m_Rect;

		// Token: 0x040020A8 RID: 8360
		protected DrivenRectTransformTracker m_Tracker;

		// Token: 0x040020A9 RID: 8361
		private Vector2 m_TotalPreferredSize = Vector2.zero;

		// Token: 0x040020AA RID: 8362
		private List<TreeNode> m_TreeLayoutElement = new List<TreeNode>();

		// Token: 0x040020AB RID: 8363
		[SerializeField]
		protected float m_Spacing;
	}
}
